package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.album.config.MinioConstantProperties;
import com.atguigu.tingshu.album.service.FileUploadService;
import com.atguigu.tingshu.album.service.VodService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author: atguigu
 * @create: 2024-11-23 09:28
 */
@Slf4j
@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioConstantProperties minioConstantProperties;

    @Autowired
    private VodService vodService;

    /**
     * 将图片文件上传到MInIO服务器
     *
     * @param file
     * @return
     */
    @Override
    public String fileUpload(MultipartFile file) {
        try {
            //1.验证文件内容是否为图片
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            if (bufferedImage == null) {
                throw new RuntimeException("图片不合法");
            }
            //2.验证图片文件大小宽高是否超过上限 900*900
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            if (width > 900 || height > 900) {
                throw new RuntimeException("图片超限，要求900*900");
            }

            //TODO 对用户上传图片进行内容审核
            Boolean flag = vodService.scanImage(file);
            if (!flag) {
                throw new RuntimeException("图片不合法");
            }

            //3.将图片文件上传到MInIO服务器，获取图片文件在线地址
            //3.1 生成全局唯一文件名称 格式：年月日/唯一文件名称.后缀
            String folderName = DateUtil.today();
            String fileName = IdUtil.getSnowflakeNextIdStr();
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            String objectName = "/" + folderName + "/" + fileName + "." + extName;
            //3.2 调用MInIO客户端上传文件
            minioClient.putObject(
                    PutObjectArgs.builder().bucket(minioConstantProperties.getBucketName()).object(objectName).stream(
                                    file.getInputStream(), file.getSize(), -1)
                            .contentType(file.getContentType())
                            .build());
            //4 拼接上传成功后文件地址
            return minioConstantProperties.getEndpointUrl() + "/" + minioConstantProperties.getBucketName() + objectName;
        } catch (Exception e) {
            log.error("[专辑服务]文件上传失败：{}", e);
            throw new RuntimeException("文件上传失败：" + e.getMessage());
        }
    }
}
